/**
 * 账户管理相关API
 */
import request from '@/utils/request'
import type { ApiResponse } from './types/common'
import type {
  UpdateProfileParams,
  ChangePasswordParams,
  ForgotPasswordParams,
  ChangeBindParams,
  VerificationParams,
  UserOverview,
  ScoreLog,
  MoneyLog,
  FavoritesResponse,
  FavoritesQueryParams,
  BatchFavoritesResponse
} from '@/types/user'

/**
 * 获取用户概览数据
 */
export const getUserOverview = (): Promise<ApiResponse<UserOverview>> => {
  return request.get('/account/overview')
}

/**
 * 获取/更新用户资料
 */
export const getUserProfile = (): Promise<ApiResponse<{
  accountVerificationType: string[]
}>> => {
  return request.get('/account/profile')
}

export const updateUserProfile = (params: UpdateProfileParams): Promise<ApiResponse<null>> => {
  return request.post('/account/profile', params)
}

/**
 * 修改密码
 */
export const changePassword = (params: ChangePasswordParams): Promise<ApiResponse<null>> => {
  return request.post('/account/changePassword', params)
}

/**
 * 找回密码
 */
export const retrievePassword = (params: ForgotPasswordParams): Promise<ApiResponse<null>> => {
  return request.post('/account/retrievePassword', params)
}

/**
 * 验证账户（邮箱）
 */
export const verifyAccount = (params: VerificationParams): Promise<ApiResponse<{
  accountVerificationToken: string
}>> => {
  return request.post('/account/verification', params)
}

/**
 * 修改绑定信息（邮箱）
 */
export const changeBind = (params: ChangeBindParams): Promise<ApiResponse<null>> => {
  return request.post('/account/changeBind', params)
}

/**
 * 获取积分日志
 */
export const getScoreLog = (params: {
  limit?: number
  page?: number
}): Promise<ApiResponse<{
  list: ScoreLog[]
  total: number
}>> => {
  return request.get('/account/integral', { params })
}

/**
 * 获取余额日志
 */
export const getMoneyLog = (params: {
  limit?: number
  page?: number
}): Promise<ApiResponse<{
  list: MoneyLog[]
  total: number
}>> => {
  return request.get('/account/balance', { params })
}

/**
 * 添加收藏
 */
export const addFavorite = (params: {
  object_id: number
  object_type?: string
}): Promise<ApiResponse<null>> => {
  return request.post('/account/addFavorite', params)
}

/**
 * 取消收藏
 */
export const removeFavorite = (params: {
  object_id: number
  object_type?: string
}): Promise<ApiResponse<null>> => {
  return request.post('/account/removeFavorite', params)
}

/**
 * 检查收藏状态
 */
export const checkFavorite = (params: {
  object_id: number
  object_type?: string
}): Promise<ApiResponse<{ is_favorite: boolean }>> => {
  return request.get('/account/checkFavorite', { params })
}

/**
 * 批量检查收藏状态
 */
export const batchCheckFavorites = (params: {
  object_ids: string
  object_type?: string
}): Promise<ApiResponse<BatchFavoritesResponse>> => {
  return request.get('/account/batchCheckFavorites', { params })
}

/**
 * 获取收藏列表
 */
export const getFavorites = (params: FavoritesQueryParams = {}): Promise<ApiResponse<FavoritesResponse>> => {
  // 兼容不同的分页参数名
  const requestParams = {
    ...params,
    limit: params.limit || params.pageSize || 10,
    pageSize: params.pageSize || params.limit || 10
  }
  return request.get('/account/getFavorites', { params: requestParams })
}
